package com.fasteat.menu.util;

import com.baidu.aip.nlp.AipNlp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author run
 * @since 2021/3/2 16:55
 */
@Component
public class MenuTagUtil {

    @Autowired
    private AipNlp lexer;

    public String getMenuTag(String title,String content){
        if (title.length() > 40){
            title = title.substring(0,40);
        }
        if (content.length() > 65535 / 2){
            content = content.substring(0,65535 / 2);
        }
        List items = (ArrayList)lexer.keyword(title, content, null).toMap().get("items");
        StringBuilder tag = new StringBuilder();
        items.forEach((item) -> {
            tag.append(((Map) item).get("tag").toString());
            tag.append(',');
        });
        String finalTag = tag.length() > 0 ? tag.toString().substring(0,tag.length() - 1) : tag.toString();

        return finalTag;
    }
}
